###############################################################################
# SuperLinks                                                                  #
# Written by Gregory Nolle (greg@nolle.co.uk)                                 #
# Copyright 2000-2003 by PlasmaPulse Solutions (http://www.plasmapulse.com)   #
###############################################################################
# ControlPanel/Output/CreateResource.pm.pl -> CreateResource module           #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Skin::CP::CreateResource;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";

use HTML::Dialog;
use HTML::Form;
use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (input => undef, error => "", @_);

  my $LANGUAGE = {
    "MISSING" => {
      "TITLE"             => qq~<li>You didn't fill in the "Title" field.~,
      "URL"               => qq~<li>You didn't fill in the "URL" field.~,
      "CATEGORY"          => qq~<li>You didn't fill in the "Category" field.~,
      "KEYWORDS"          => qq~<li>You didn't fill in the "Keywords" field.~,
      "CONTACT_NAME"      => qq~<li>You didn't fill in the "Contact Name" field.~,
      "CONTACT_EMAIL"     => qq~<li>You didn't fill in the "Contact Email" field.~,
      "PASSWORD"          => qq~<li>You didn't fill in the "Password" field.~,
      "IMAGE_URL"         => qq~<li>You didn't fill in the "Image URL" field.~,
      "IMAGE_UPLOAD"      => qq~<li>You didn't fill in the "Image Upload" field.~
    },
    "TOOLONG" => {
      "TITLE"             => qq~<li>The value for the "Title" field must be 128 characters or less.~,
      "URL"               => qq~<li>The value for the "URL" field must 256 characters or less.~,
      "DESCRIPTION"       => qq~<li>The value for the "Description" field must be 512 characters or less.~,
      "KEYWORDS"          => qq~<li>The value for the "Keywords" field must be 512 characters or less.~,
      "IMAGE_URL"         => qq~<li>The value for the "Image URL" field must be 512 characters or less.~,
      "CONTACT_NAME"      => qq~<li>The value for the "Contact Name" field must be 128 characters or less.~,
      "CONTACT_EMAIL"     => qq~<li>The value for the "Contact Email" field must be 128 characters or less.~,
      "PASSWORD"          => qq~<li>The value for the "Password" field must be 64 characters or less.~
    },
    "INVALID" => {
      "URL"               => qq~<li>The value you entered for the "URL" field is invalid.~,
      "CONTACT_EMAIL"     => qq~<li>The value you entered for the "Contact Email" field is invalid.~,
      "CATEGORY"          => qq~<li>The value you entered for the "Category" field is invalid.~,
      "IMAGE_UPLOAD_SIZE" => qq~<li>The Image Upload file must be $GENERAL->{'UPLOAD_MAXSIZE'}KB or less.~,
      "IMAGE_UPLOAD_EXT"  => qq~<li>The Image Upload file must have one of the following extensions: ~.join(", ", @{$GENERAL->{'UPLOAD_EXTENSIONS'}}).qq~.~,
      "IMAGE_UPLOAD_TYPE" => qq~<li>The Image Upload file you specified does not appear to be an image.~,
      "IMAGE_UPLOAD_DIM"  => qq~<li>The Image Upload file's dimensions must be less than or equal to $GENERAL->{'UPLOAD_MAXWIDTH'}W x $GENERAL->{'UPLOAD_MAXHEIGHT'}H.~
    },
    "ERROR"         => qq~<p><font class="error">There were errors:<ul>[%error%]</ul></font>~
  };

  foreach my $field (@{ $GENERAL->{'RESOURCE_EXTRA'} }) {
    $LANGUAGE->{'MISSING'}->{'EXTRA_'.$field->{'ID'}} = qq~<li>You didn't fill in the "$field->{'DESCRIPTION'}" field.~ if ($field->{'REQUIRED'});
    $LANGUAGE->{'TOOLONG'}->{'EXTRA_'.$field->{'ID'}} = qq~<li>The value for the "$field->{'DESCRIPTION'}" field must be $field->{'SIZE'} characters or less.~ if ($field->{'SIZE'});
  }

  $in{'error'} = &Standard::ProcessError(LANGUAGE => $LANGUAGE, ERROR => $in{'error'});

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();
  my $Form   = HTML::Form->new();

  my $Body = $Dialog->TableRow(items => [{ body => "Please fill out all the required (<font class=\"form-required\"><b>*</b></font>) fields:".$in{'error'}, colspan => "2" }]);

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_TITLE",
    subject   => "Title",
    required  => 1
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_URL",
    subject   => "URL",
    required  => 1
  );

  my @options = &Standard::MapArray({ value => "\${ID}", label => "/ \${FULL_TITLE}" }, $in{'input'}->{'CATEGORIES'});
  
  $Body .= $Dialog->FormSelectBox(
    name      => "FORM_CATEGORY",
    subject   => "Category",
    required  => 1,
    items     => \@options
  );
  
  $Body .= $Dialog->FormTextArea(
    name      => "FORM_DESCRIPTION",
    subject   => "Description",
    rows      => 3
  );

  $Body .= $Dialog->FormTextArea(
    name      => "FORM_KEYWORDS",
    subject   => "Keywords",
    rows      => 3,
    required  => 1
  );

  my $Image  = "<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">";
     $Image .= "<tr><td width=\"1\" nowrap>";
     $Image .= $Form->Radio(name => "FORM_IMAGE_TYPE", value => "NONE", items => [{ value => "NONE" }]);
     $Image .= "</td><td colspan=\"2\" width=\"100%\"><font class=\"form-label\">No image.</font></td></tr>";
     $Image .= "<tr><td width=\"1\" nowrap>";
     $Image .= $Form->Radio(name => "FORM_IMAGE_TYPE", value => "NONE", items => [{ value => "URL" }]);
     $Image .= "</td><td width=\"1\" nowrap><font class=\"form-label\">URL:</font></td>";
     $Image .= "<td width=\"100%\">";
     $Image .= $Form->TextBox(name => "FORM_IMAGE_URL", value => "http://");
     $Image .= "</td></tr>";
     $Image .= "<tr><td width=\"1\" nowrap>";
     $Image .= $Form->Radio(name => "FORM_IMAGE_TYPE", value => "NONE", items => [{ value => "UPLOAD" }]);
     $Image .= "</td><td width=\"1\" nowrap><font class=\"form-label\">Upload:</font></td>";
     $Image .= "<td width=\"100%\">";
     $Image .= $Form->FileBox(name => "FORM_IMAGE_UPLOAD");
     $Image .= "</td></tr></table>";

  $Body .= $Dialog->FormTextBox(
    value     => $Image,
    subject   => "Image"
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_CONTACT_NAME",
    subject   => "Contact Name",
    required  => 1
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_CONTACT_EMAIL",
    subject   => "Contact Email",
    required  => 1
  );
  
  $Body .= $Dialog->FormCheckBox(
    name      => "FORM_SHOW_EMAIL",
    value     => "1",
    items     => [
      { value => "1", label => "Show Email?" }
    ]
  );

  $Body .= $Dialog->FormCheckBox(
    name      => "FORM_MAILING_LIST",
    value     => "1",
    items     => [
      { value => "1", label => "Allow email updates?" }
    ]
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_PASSWORD",
    subject   => "Password",
    required  => 1
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_WEIGHTING",
    value     => "0",
    subject   => "Weighting"
  );

  $Body .= $Dialog->FormRadio(
    name      => "FORM_STATUS",
    value     => "1",
    subject   => "Status",
    items     => [
      { value => "0", label => "Inactive" },
      { value => "1", label => "Active" }
    ], join => "&nbsp;"
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_HITS",
    value     => "0",
    subject   => "Hits"
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_RATING",
    value     => "0",
    subject   => "Rating"
  );

  $Body .= $Dialog->FormCheckBox(
    name      => "FORM_FLAGS",
    subject   => "Flags",
    items     => [
      { value => "MODIFIED", label => "Modified" },
      { value => "DEAD_LINK", label => "Dead Link" },
      { value => "DELETED", label => "Deleted" },
      { value => "DUPLICATE", label => "Duplicate URL" }
    ], join => "<br>"
  );

  if (scalar(@{ $GENERAL->{'RESOURCE_EXTRA'} }) >= 1) {
    $Body .= $Dialog->TableHeader(titles => [{ title => "Extra", align => "left", colspan => 2 }]);
  
    foreach my $field (@{ $GENERAL->{'RESOURCE_EXTRA'} }) {
      if ($field->{'CUSTOM'}) {
        my $value = &Standard::Substitute(
          INPUT     => \$field->{'CUSTOM'},
          STANDARD  => 1
        );
        $Body .= $Dialog->FormTextBox(
          value     => $value,
          subject   => $field->{'DESCRIPTION'},
          required  => $field->{'REQUIRED'}
        );
      } else {
        $Body .= $Dialog->FormTextBox(
          name      => "FORM_EXTRA_".$field->{'ID'},
          subject   => $field->{'DESCRIPTION'},
          required  => $field->{'REQUIRED'}
        );
      }
    }
  }

  $Body .= $Dialog->FormButton(
    items => [
      { type => "submit", value => "Add" },
      { type => "reset", value => "Cancel" }
    ], join => "&nbsp;"
  );

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body      => $Body,
    header    => "Create a Resource",
    form      => 1,
    multipart => 1,
    hiddens   => [
      { name => "Referer", value => $SL::QUERY{'Referer'} },
      { name => "action", value => "DoCreateResource" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} }
    ]
  );
  
  return $Body;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (input => undef, @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body = $Dialog->TableRow(items => [{ body => "The following category has been created:", colspan => 2 }]);

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'RECORD'}->{'ID'},
    subject   => "ID"
  );

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'RECORD'}->{'TITLE'},
    subject   => "Title"
  );
  
  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'RECORD'}->{'URL'},
    subject   => "URL"
  );
  
  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'CATEGORY'}->{'FULL_TITLE'},
    subject   => "Category"
  );
    
  $Body .= $Dialog->FormTextArea(
    value     => $in{'input'}->{'RECORD'}->{'DESCRIPTION'},
    subject   => "Description"
  );

  $Body .= $Dialog->FormTextArea(
    value     => $in{'input'}->{'RECORD'}->{'KEYWORDS'},
    subject   => "Keywords"
  );

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'RECORD'}->{'IMAGE'}.($in{'input'}->{'RECORD'}->{'UPLOAD_FILENAME'} ? " (Uploaded)" : ""),
    subject   => "Image"
  );

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'RECORD'}->{'CONTACT_NAME'},
    subject   => "Contact Name"
  );

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'RECORD'}->{'CONTACT_EMAIL'},
    subject   => "Contact Email"
  );
  
  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'RECORD'}->{'PASSWORD'},
    subject   => "Password"
  );

  if (scalar(@{ $GENERAL->{'RESOURCE_EXTRA'} }) >= 1) {
    $Body .= $Dialog->TableHeader(titles => [{ title => "Extra", align => "left", colspan => 2 }]);
  
    foreach my $field (@{ $GENERAL->{'RESOURCE_EXTRA'} }) {
      if ($field->{'CUSTOM_DONE'}) {
        my $value = &Standard::Substitute(
          INPUT     => \$field->{'CUSTOM_DONE'},
          STANDARD  => 1,
          FIELDS    => {
            "record"  => $in{'input'}->{'RECORD'}
          }
        );
        $Body .= $Dialog->FormTextBox(
          value     => $value,
          subject   => $field->{'DESCRIPTION'}
        );
      } else {
        $Body .= $Dialog->FormTextBox(
          value     => $in{'input'}->{'RECORD'}->{'extra'}->{$field->{'ID'}},
          subject   => $field->{'DESCRIPTION'}
        );
      }
    }
  }

  $Body .= $Dialog->TableDivider(colspan => 2);
  $Body .= $Dialog->TableRow(items => [{ body => qq~Click <a href="~.($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Resources&SID=$SL::ADMIN{'SID'}").qq~">here</a> if you're not redirected in 5 seconds.~, colspan => 2 }]);

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body    => $Body,
    header  => "Create a Resource",
    metas   => [
      { http_equiv => "refresh", content => "5; url=".($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Resources&SID=$SL::ADMIN{'SID'}") }
    ]
  );
  
  return $Body;
}

1;